package com.shop2cn.iapi.shenqisettlement.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.GetUserBandingBankCardInfoListReq;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetUserBandingBankCardInfoListResp;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetUserBandingBankCardInfoResp;
import com.shop2cn.iapi.shenqisettlement.service.GetUserBandingBankCardInfoListCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.shenqisettlement.TeFeeRateConfig;
import com.ymttest.database.model.shenqisettlement.TeWithdrawFeeConfig;
import com.ymttest.database.model.shenqisettlement.UserBankAccount;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.shenqisettlement.TeFeeRateConfigWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.TeWithdrawFeeConfigWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.UserBankAccountWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.MapUtil;

public class Ts_GetUserBandingBankCardInfoList {
	
	private static GetUserBandingBankCardInfoListReq getUserBandingBankCardInfoListReq = null;
	private static GetUserBandingBankCardInfoListCall getUserBandingBankCardInfoListCall = null;

	private static String key = "123456";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户绑定的银行卡信息列表");
	}

	@Before
	public void caseUp() {

		getUserBandingBankCardInfoListReq = new GetUserBandingBankCardInfoListReq();
		getUserBandingBankCardInfoListCall = new GetUserBandingBankCardInfoListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("api/getUserBandingBankCardInfoList");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify() throws Exception {

		GetUserBandingBankCardInfoListResp getUserBandingBankCardInfoList = getUserBandingBankCardInfoListCall
				.getResult();
		
		long actualUserId = getUserBandingBankCardInfoListReq.getUserId();
		String actualCurrency = new mch_infoWapper()
				.selectby(MapUtil.hashMap("mchId", actualUserId)).get(0)
				.getCurrency();
		
		UserBankAccountWrapper userBankAccountWrapper = new UserBankAccountWrapper();
		List<UserBankAccount> actualBankAccountList = new ArrayList<>();
		
		if (null != getUserBandingBankCardInfoListReq.getPayChannelType())
			actualBankAccountList = userBankAccountWrapper
					.selectByUserIdAndPayChannelType(actualUserId,
							getUserBandingBankCardInfoListReq
									.getPayChannelType());
		else
			actualBankAccountList = userBankAccountWrapper
					.selectByUserId(actualUserId);
		
		int i = 0;
		
		for (UserBankAccount actualBankAccount : actualBankAccountList) {
			
			GetUserBandingBankCardInfoResp expectResp = getUserBandingBankCardInfoList
					.getGetUserBandingBankCardInfoList().get(i);

			HashMap<String, String> expectMap = expectResp
					.getBankRequiredFieldMap();
			
			for (HashMap.Entry<String, String> entry : expectMap.entrySet()) {

				if (entry.getKey().equalsIgnoreCase("bankName"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getBank_name(), "验证bank_name");
				else if (entry.getKey().equalsIgnoreCase("bankBranch"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getBank_branch(), "验证bank_branch");
				else if (entry.getKey().equalsIgnoreCase("bankSwiftCode"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getBank_swift_code(),
							"验证bank_swift_code");
				else if (entry.getKey().equalsIgnoreCase("bankRoutingNumber"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getBank_routing_number(),
							"验证bank_routing_number");
				else if (entry.getKey().equalsIgnoreCase("bankBsbCode"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getBank_routing_number(),
							"验证bank_routing_number");
				else if (entry.getKey().equalsIgnoreCase("holerName"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getHoler_name(), "验证holer_name");
				else if (entry.getKey().equalsIgnoreCase("country"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getCountry(), "验证country");
				else if (entry.getKey().equalsIgnoreCase("holerDocNumber"))
					Logger.verifyEquals(
							entry.getValue(),
							CipherUtil.decryptAES(key,
									actualBankAccount.getHoler_doc_number()),
							"验证holer_doc_number");
				else if (entry.getKey().equalsIgnoreCase("holerContact"))
					Logger.verifyEquals(
							entry.getValue(),
							CipherUtil.decryptAES(key,
									actualBankAccount.getHoler_contact()),
							"验证holer_contact");
				else if (entry.getKey().equalsIgnoreCase("holerDob"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getHoler_dob(), "验证holer_dob");
				else if (entry.getKey().equalsIgnoreCase("holerCountry"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getHoler_country(), "验证holer_country");
				else if (entry.getKey().equalsIgnoreCase("holerAddress"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getHoler_address(), "验证holer_address");
				else if (entry.getKey().equalsIgnoreCase("iban"))
					Logger.verifyEquals(entry.getValue(),
							actualBankAccount.getIban(), "验证iban");
			}

			Logger.verifyEquals(expectResp.getCountry(),
					actualBankAccount.getCountry(), "验证country");
			Logger.verifyEquals(expectResp.getCurrency(), actualCurrency,
					"验证currency");
			Logger.verifyEquals(expectResp.getAccountAuditStatus(),
					actualBankAccount.getAudit_status(), "验证accountAuditStatus");
			Logger.verifyEquals(expectResp.getAccountAuditFailReason(),
					actualBankAccount.getFail_reason(), "验证accountAuditFailReason");

			TeWithdrawFeeConfigWrapper teWithdrawFeeConfigWrapper = new TeWithdrawFeeConfigWrapper();
			TeWithdrawFeeConfig teWithdrawFeeConfig = teWithdrawFeeConfigWrapper
					.selectByCountry(expectResp.getCountry());

			DataUtil.verifyDecimalEquals(expectResp.getTeFeeUsd(),
					teWithdrawFeeConfig.getTransaction_fee(), "验证TeFeeUsd");
			DataUtil.verifyDecimalEquals(expectResp.getTeMaxWithdrawAmountUsd(),
					teWithdrawFeeConfig.getMax_size_fee(),
					"验证TeMaxWithdrawAmountUsd");

			TeFeeRateConfigWrapper teFeeRateConfigWrapper = new TeFeeRateConfigWrapper();
			TeFeeRateConfig teFeeRateConfig = teFeeRateConfigWrapper
					.selectByPrimaryKey(actualCurrency);
			
			int scale = 0;
			
			if (actualCurrency.equalsIgnoreCase("JPY"))
				scale = 0;
			else
				scale = 2;

			DataUtil.verifyDecimalEquals(
					expectResp.getMaxWithdrawAmount(),
					teFeeRateConfig.getRate().multiply(
							expectResp.getTeMaxWithdrawAmountUsd()),
					"验证maxWithdrawAmount");
			DataUtil.verifyDecimalEquals(expectResp.getFee(), teFeeRateConfig
					.getRate().multiply(expectResp.getTeFeeUsd()).setScale(scale, BigDecimal.ROUND_HALF_UP), "验证fee");
			
			i ++;
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfoList_001() {
		Logger.start(true, "获取用户绑定银行卡信息列表");
		try {

			getUserBandingBankCardInfoListReq.setUserId(502216286L);

			getUserBandingBankCardInfoListCall
					.setData(getUserBandingBankCardInfoListReq);
			getUserBandingBankCardInfoListCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoListCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoListCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfoList_002() {
		Logger.start(true, "获取多店铺多用户绑定的hipopay银行卡信息");
		try {

			getUserBandingBankCardInfoListReq.setUserId(502216286L);
			getUserBandingBankCardInfoListReq.setPayChannelType(0);

			getUserBandingBankCardInfoListCall
					.setData(getUserBandingBankCardInfoListReq);
			getUserBandingBankCardInfoListCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoListCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoListCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfoList_003() {
		Logger.start(true, "获取多店铺多用户绑定的hipopay银行卡信息");
		try {

			getUserBandingBankCardInfoListReq.setUserId(501413599L);
			getUserBandingBankCardInfoListReq.setPayChannelType(1);

			getUserBandingBankCardInfoListCall
					.setData(getUserBandingBankCardInfoListReq);
			getUserBandingBankCardInfoListCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoListCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoListCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfoList_004() {
		Logger.start(true, "userId为0");
		try {

			getUserBandingBankCardInfoListReq.setUserId(0L);

			getUserBandingBankCardInfoListCall
					.setData(getUserBandingBankCardInfoListReq);
			getUserBandingBankCardInfoListCall.callService();

			Logger.verifyEquals(false,
					getUserBandingBankCardInfoListCall.isSuccess(), "验证success");
			Logger.verifyEquals("userId:must be greater than or equal to 1",
					getUserBandingBankCardInfoListCall.getResultMessage(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
